package com.cxyd.modules.wcs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxyd.common.constant.WcsConstants;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.ApiResult;
import com.cxyd.common.utils.ConvertUtils;
import com.cxyd.common.utils.ProcResult;
import com.cxyd.common.utils.StringUtils;
import com.cxyd.modules.api.dto.PinStateDTO;
import com.cxyd.modules.wcs.dao.ContDao;
import com.cxyd.modules.wcs.dao.PinDao;
import com.cxyd.modules.wcs.dao.StnMappingDao;
import com.cxyd.modules.wcs.dto.PinDTO;
import com.cxyd.modules.wcs.entity.ContEntity;
import com.cxyd.modules.wcs.entity.PinEntity;
import com.cxyd.modules.wcs.entity.StnMappingEntity;
import com.cxyd.modules.wcs.service.PinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PinServiceImpl extends CrudServiceImpl<PinDao, PinEntity, PinDTO> implements PinService {
    @Autowired
    private ContDao contDao;
    @Autowired
    private StnMappingDao stnMappingDao;

    @Override
    public QueryWrapper<PinEntity> getWrapper(Map<String, Object> params) {
        String pinNo = (String) params.get("pinNo");
        String contNo = (String) params.get("contNo");
        String contType = (String) params.get("contType");
        QueryWrapper<PinEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(pinNo), "pin_no", pinNo);
        wrapper.eq(StringUtils.isNotBlank(contNo), "cont_no", contNo);
        wrapper.eq(StringUtils.isNotBlank(contType), "cont_type", contType);
        return wrapper;
    }

    @Override
    public ProcResult updatePin(PinDTO dto) {
        if (dto == null) {
            return new ProcResult().result("pin_update_001", "参数错误");
        }
        PinEntity newPin = ConvertUtils.sourceToTarget(dto, PinEntity.class);
        newPin.setComments("人为修改型号");
        baseDao.updateById(newPin);
        return new ProcResult().result("0", "型号更新完成！");
    }

    @Override
    public ApiResult switchPinNo(PinStateDTO dto) {
        // 根据mapCode切换PIN型号
        if (StringUtils.isBlank(dto.getMapCode())) {
            return new ApiResult().feedBack("型号编码不能为空!", dto.getRequestPk());
        }
        // 根据mapCode查询PIN信息
        QueryWrapper<PinEntity> pinQW = new QueryWrapper<>();
        pinQW.eq("pin_no", dto.getMapCode());
        List<PinEntity> pinList = baseDao.selectList(pinQW);
        if (pinList == null || pinList.size() == 0) {
            return new ApiResult().feedBack("型号【" + dto.getMapCode() + "】在MCS未配置！无法同步", dto.getRequestPk());
        }
        // 循环处理该型号下每个料架车
        for (PinEntity pin : pinList) {
            // 根据contNo查询料架车
            ContEntity cont = contDao.selectOne(new QueryWrapper<ContEntity>().eq("cont_no", pin.getContNo()));
            if (cont == null) {
                continue;
            }
            // 更新料架车的型号
            cont.setPinNo(pin.getPinNo());
            cont.setContType(pin.getContType());
            cont.setUpdater(WcsConstants.MES_USER_ID);
            cont.setComments("MES切换型号【" + dto.getMapCode() + "】");
            contDao.updateById(cont);

            // 同步缓存位置上的料架车信息
            QueryWrapper<StnMappingEntity> contNoQW = new QueryWrapper<>();
            contNoQW.eq("cont_no", cont.getContNo())
                    .isNotNull("wms_station").last("limit 1");
            StnMappingEntity station = stnMappingDao.selectOne(contNoQW);
            if (station != null) {
                station.setContType(pin.getContType());
                station.setIsFull("false");
                stnMappingDao.updateById(station);
            }
        }
        return new ApiResult().feedBack(dto.getRequestPk());
    }
}
